//
//  recommendController.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/1.
//  Copyright © 2015年 周莹. All rights reserved.
//

/*
 
 1.你使用过Objective-C的运行时编程(Runtime Programming)么?如果使用过，你用它做了什么?你还能记得你所使用的相关的头文件或者某些方法的名称吗?
 
 　　2.你实现过多线程的Core Data么?NSPersistentStoreCoordinator，NSManagedObjectContext和NSManagedObject中的哪些需要在线程中创建或者传递?你是用什么样的策略来实现的?
 
 　　3.Core开头的系列的内容。是否使用过CoreAnimation和CoreGraphics。UI框架和CA，CG框架的联系是什么?分别用CA和CG做过些什么动画或者图像上的内容。(有需要的话还可以涉及Quartz的一些内容)
 
 　　4.是否使用过CoreText或者CoreImage等?如果使用过，请谈谈你使用CoreText或者CoreImage的体验。
 
 　　5.NSNotification和KVO的区别和用法是什么?什么时候应该使用通知，什么时候应该使用KVO，它们的实现上有什么区别吗?如果用protocol和delegate(或者delegate的Array)来实现类似的功能可能吗?如果可能，会有什么潜在的问题?如果不能，为什么?(虽然protocol和delegate这种东西面试已经面烂了…)
 
 　　6.你用过NSOperationQueue么?如果用过或者了解的话，你为什么要使用NSOperationQueue，实现了什么?请描述它和GCD的区别和类似的地方(提示：可以从两者的实现机制和适用范围来描述)。
 
 　　7.既然提到GCD，那么问一下在使用GCD以及block时要注意些什么?它们两是一回事儿么?block在ARC中和传统的MRC中的行为和用法有没有什么区别，需要注意些什么?
 
 　　8.您是否做过异步的网络处理和通讯方面的工作?如果有，能具体介绍一些实现策略么?
 
 　　9.对于Objective-C，你认为它最大的优点和最大的不足是什么?对于不足之处，现在有没有可用的方法绕过这些不足来实现需求。如果可以的话，你有没有考虑或者实践过重新实现OC的一些功能，如果有，具体会如何做?
 
 　　10.你实现过一个框架或者库以供别人使用么?如果有，请谈一谈构建框架或者库时候的经验;如果没有，请设想和设计框架的public的API，并指出大概需要如何做、需要注意一些什么方面，来使别人容易地使用你的框架。
 
 　　以上10个问题对于初级或者刚接触iOS的开发者来说，肯定是过于难了。想要答出全部问题，可能需要至少两到三年的Cocoa/CocoaTouch开发经验。
 
 */

#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height

#import "RadioDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "recommendController.h"
#import "MusicViewController.h"
#import "recommendButton.h"
#import "recommendHelper.h"
#import "ScrollViewModel.h"
#import "recommendModel.h"
#import "scrollViewCell.h"
#import "recommendCell.h"
#import "sectionModel.h"
#import "footView.h"
#import "TopModel.h"
#import "Colours.h"
#import "RadioDetailViewController.h"
#import "radioModel.h"
#import "songListModel.h"


@interface recommendController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property(nonatomic,strong)recommendButton *middleButton;
@property(nonatomic,strong)UITableView *bigTableView;

@property(nonatomic,retain)UIScrollView *mainScrollView;
@property(nonatomic,retain)UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
//@property(nonatomic,strong)NSMutableArray *reciveArray;
//@property (nonatomic, strong) NSArray *cellDataArray;
//@property (nonatomic, strong) NSMutableArray *reMutableArray;



@end

@implementation recommendController


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    [[recommendHelper sharedInstancetype] downLoadDataFinished:^{
        
        [self.bigTableView reloadData];
    }];
    
    //    [self LoadCellData];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    self.bigTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 112) style:UITableViewStylePlain];
    self.bigTableView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.bigTableView];
    self.bigTableView.delegate = self;
    self.bigTableView.dataSource = self;
    [self.bigTableView registerClass:[recommendCell class] forCellReuseIdentifier:@"recommend"];
    
}

-(void)timerAction
{
    CGFloat pageWidth = self.mainScrollView.frame.size.width;
    
    int page = (int)(self.mainScrollView.contentOffset.x / pageWidth) + 1;//根据坐标算页数
    // NSLog(@"============%d",page);
    self.pageControl.currentPage = page;//页数赋值给pageControl的当前页
    if (page == 7) {
        self.pageControl.currentPage = 0;
        [self.mainScrollView setContentOffset:CGPointMake(0, 0)];
    }else {
        [self.mainScrollView setContentOffset:CGPointMake(self.mainScrollView.contentOffset.x + [UIScreen mainScreen].bounds.size.width, self.mainScrollView.contentOffset.y) animated:YES];
//        [self.mainScrollView setContentSize:CGSizeMake(self.mainScrollView.contentOffset.x + [UIScreen mainScreen].bounds.size.width, self.mainScrollView.contentOffset.y)];
        
        NSLog(@"%f",self.mainScrollView.contentOffset.x);
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 11;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 )
    {
        return 1;
    }
    else
    {
        //        NSArray *rowArray = [recommendHelper sharedInstancetype].recommendArray[section];
        //        return rowArray.count;
        
        
        
        sectionModel *model = [recommendHelper sharedInstancetype].sectionArray[1];
        return model.list.count;
        
        
        //        NSLog(@"======= ==== ===%ld",[recommendHelper sharedInstancetype].sectionArray.count);
        //        return 3;
    }
    
}


#pragma mark - 头部

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    // // // // // // // // // // // 高度在下一个代理方法里设置
    
    if (section == 0)
    {
        UIView *testView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 150)];
        self.mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 150)];
        self.automaticallyAdjustsScrollViewInsets = NO;
        [testView addSubview:self.mainScrollView];
        
        
        self.mainScrollView.contentSize = CGSizeMake(screenWidth * 7, 0);
        self.mainScrollView.delegate = self;
        // 放置图片
        for (int i = 0;  i < 7; i ++)
        {
            
            if ([recommendHelper sharedInstancetype].scrollArray.count < 7) return nil;
            ScrollViewModel *model = [recommendHelper sharedInstancetype].scrollArray[i];
            UIImageView *imageView = [[UIImageView alloc]init];
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.pic ]];
            imageView.frame = CGRectMake(i * screenWidth, 0,screenWidth ,150);
            [self.mainScrollView addSubview:imageView];
           
        }
        
        // 设置 scrollView 的属性，整页滑动
        self.mainScrollView.pagingEnabled = YES;
        // 按照 frame宽度大小 翻动 考
        
        
        // 设置 scrollView 能否滑动
        // self.mainScrollView.scrollEnabled = NO;
        
        
        /************* 如何判断 这个软件是第一次运行 **************/
        // 定义变量来标记
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 100, screenWidth, 30)];
        self.pageControl.backgroundColor = [UIColor clearColor];
        // 必须加载self上，加到scrollView 就划走了
        // 告诉有几个小点
        self.pageControl.numberOfPages = 7;
        // 改变小球的颜色  未选中
        self.pageControl.pageIndicatorTintColor = [UIColor greenColor];
        // 选中状态
        self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        // 设置默认选中第几个小点
        self.pageControl.currentPage = 0;
        
        
        [testView addSubview:self.pageControl];
        
        // 控制器里绑定方法
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        
        testView.backgroundColor = [UIColor grayColor];
        
        return testView;
        
    }
    //    else if(section == 10)
    //    {
    //
    //        footView *footerView = [[footView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 25) withLeaderImage:@"箭头" withTextLabel:@"更多分类" withRightButton:@"更多"];
    //        footerView.backgroundColor = [UIColor whiteColor];
    //        return footerView;
    //    }
    else
    {
        sectionModel *model = [recommendHelper sharedInstancetype].sectionArray[section - 1];
        footView *footerView = [[footView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 25) withLeaderImage:@"箭头" withTextLabel:model.title withRightButton:@"更多"];
        footerView.backgroundColor = [UIColor whiteColor];
        return footerView;
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 150;    // 一开始 是 40 他是 scrollView 的高度 就是 header 的高度
    
    if (scrollView.contentOffset.y <= sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 150;
    }
    else
    {
        return 25;
    }
}

#pragma mark - 尾部

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 12)];
    footView.backgroundColor = [UIColor lightGrayColor];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 14;
}

////cell点击事件
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    sectionModel *model1 = [recommendHelper sharedInstancetype].sectionArray[indexPath.section - 1];
//   recommendModel *model = model1.list[indexPath.row];
////    
//    RadioDetailViewController *radio = [[RadioDetailViewController alloc] init];
//    [self.navigationController pushViewController:radio animated:YES];
//
//    radio.albumId = model2.albumId;
//    radio.position = [NSString stringWithFormat:@"%ld",indexPath.row];
//
//}
#pragma mark - scrollview 代理方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
    self.pageControl.currentPage = page;
}



//- (void) LoadCellData {
//
//    [recommendHelper downLoadDataFinished:^(id array) {
//
//        NSArray *Array = [sectionModel objectArrayWithKeyValuesArray:array];
//
//        for (sectionModel *dataModel  in Array) {
//
//            sectionModel *model = [[sectionModel alloc] init];
//                model = dataModel;
//            [self.reciveArray addObject:model];
//        }
//        self.cellDataArray = [NSArray arrayWithArray:self.reciveArray];
//
//
//    } fail:^(id error) {
//
//    }];
//
//}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"flag"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"flag"];
        // 除了 subtitle 之外没有 detaillabel 这一说
    }
    
    if (indexPath.section == 0 && indexPath.row == 0 )
    {
        TopModel *model = [recommendHelper sharedInstancetype].topArray.firstObject;
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.coverPath]];
        cell.textLabel.text = model.title;
        cell.imageView.layer.cornerRadius = 30;
        cell.imageView.clipsToBounds = YES;
        cell.detailTextLabel.text = model.subtitle;
        cell.userInteractionEnabled = NO;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        //  NSLog(@"!!!%@",cell.detailTextLabel);
    }
    else
    {
        recommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recommend" forIndexPath:indexPath];
        cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        
        sectionModel *model1 = [recommendHelper sharedInstancetype].sectionArray[indexPath.section - 1];
        recommendModel *model = [[recommendModel alloc]init];
        [model setValuesForKeysWithDictionary:model1.list[indexPath.row]];
        cell.titleLabel.text = model.title;
        cell.introLabel.text = model.intro;
        cell.userLabel.text = [NSString stringWithFormat:@"%@",model.playsCounts];
        cell.setLabel.text = [NSString stringWithFormat:@"%@集", model.tracksCounts];
        [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
        
        if ([cell.userLabel.text isEqualToString:@"0"]) {
            cell.userImage.hidden = YES;
            cell.userLabel.text = @"";
            cell.setImage.frame = CGRectMake(95, 78, 10, 10);
            cell.setLabel.frame = CGRectMake(112, 75, 65, 15);
        }
        // NSLog(@"%@", cell.userLabel.text);
       //  NSLog(@"************%@",[NSString stringWithFormat:@"%@", model.albumId]);
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 && indexPath.section == 0)
    {
        return 80;
    }
    else
    {
        return 100;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    sectionModel *model1 = [recommendHelper sharedInstancetype].sectionArray[indexPath.section - 1];
    recommendModel *model = [[recommendModel alloc]init];
    [model setValuesForKeysWithDictionary:model1.list[indexPath.row]];

   
    RadioDetailViewController *radio = [[RadioDetailViewController alloc] init];
    [self.navigationController pushViewController:radio animated:YES];
    
  //  NSLog(@"************%@",[NSString stringWithFormat:@"%@", model.albumId]);
    
    radio.albumId = [NSString stringWithFormat:@"%@", model.albumId];
    radio.position = [NSString stringWithFormat:@"%ld",indexPath.row];

    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


@end
